//创建蛇对象
class Snake {
    constructor(width, height, direction) {
        this.width = width || 20;
        this.height = height || 20;

        //构建蛇身体
        this.body = [
            { x: 3, y: 2, color: 'red' },
            { x: 2, y: 2, color: 'green' },
            { x: 1, y: 2, color: 'green' },
            { x: 0, y: 2, color: 'green' },
        ]
        //设置方向
        this.direction = direction || 'right'
    }
    init(scope) {
        //遍历蛇身体的数组
        for (let i = 0; i < this.body.length; i++) {
            let item = this.body[i];
            let div = document.createElement("div");
            scope.append(div)
            //设置蛇身体的样式
            div.style.position = 'absolute';
            div.style.border = '1px solid';
            div.style.boxSizing = 'border-box';
            div.style.width = this.width + 'px';
            div.style.height = this.height + 'px';
            div.style.backgroundColor = item.color;

            //蛇坐标
            div.style.left = item.x * this.width + 'px'
            div.style.top = item.y * this.height + 'px'
            snakeArr.push(div)
        }
    }
    eat(food) {
        if (food.x / food.width == this.body[0].x && food.y / food.height == this.body[0].y) {
            food.update()
            this.body.push({ x: food.x / food.width, y: food.y / food.height, color: `rgb(${Math.random() * 255},${Math.random() * 255},${Math.random() * 255})` })
        }
    }
    stop(scope) {//当有任何一个条件不符合时返回true，表示停止移动
        //当头碰到身体时，游戏结束
        for (let i = 1; i < this.body.length; i++) {
            // console.log(this.body[0].x == this.body[i].x && this.body[0].y == this.body[i].y)
            if (this.body[0].x == this.body[i].x && this.body[0].y == this.body[i].y) {
                return true
            }
        }
        // console.log(this.body[0].x)
        if (this.body[0].x < (scope.offsetWidth / this.width)
            && this.body[0].x >= 0
            && this.body[0].y < (scope.offsetHeight / this.height)
            && this.body[0].y >= 0) {
            return false
        }
        return true
    }
    move(scope, food, snake) {
        remove(snakeArr)
        //假设往右走，最后蛇尾移动到上一个位置后面以此类推，蛇头就直接 + 1
        let i = this.body.length - 1
        for (; i > 0; i--) {
            this.body[i].x = this.body[i - 1].x
            this.body[i].y = this.body[i - 1].y
            // if(this.body[0].x == this.body[i].x && this.body[0].y == this.body[i].y){
            //     conosle.log(111)
            //     timer =null
            // }
        }
        //
        switch (this.direction) {
            case 'right':
                this.body[0].x += 1;
                break;
            case 'left':
                this.body[0].x -= 1;
                break;
            case 'up':
                this.body[0].y -= 1;
                break;
            case 'down':
                this.body[0].y += 1;
                break;
        }
        snake.eat(food)
        snake.init(scope)
    }
}
